|
1
|
|
|
import path from 'path' |
|
2
|
|
|
import extend from 'extend' |
|
3
|
|
|
|
|
4
|
|
|
import { |
|
5
|
|
|
abeExtend, |
|
6
|
|
|
Manager, |
|
7
|
|
|
config, |
|
8
|
|
|
coreUtils, |
|
9
|
|
|
cmsData, |
|
10
|
|
|
cmsOperations |
|
11
|
|
|
} from '../../' |
|
12
|
|
|
|
|
13
|
|
|
const duplicate = function(oldPostUrl, template, newPath, name, req, isUpdate = false) { |
|
14
|
|
|
const p = new Promise((resolve, reject) => { |
|
15
|
|
|
abeExtend.hooks.instance.trigger('beforeDuplicate', oldPostUrl, template, newPath, name, req, isUpdate) |
|
16
|
|
|
|
|
17
|
|
|
let json = {} |
|
18
|
|
|
let revisions = [] |
|
19
|
|
|
const newPostUrl = path.join(newPath, coreUtils.slug.clean(name)) |
|
20
|
|
|
if(oldPostUrl != null) { |
|
21
|
|
|
const files = Manager.instance.getList() |
|
22
|
|
|
const oldPostDataPath = path.join(config.root, config.data.url, oldPostUrl.replace('.' + config.files.templates.extension, '.json')) |
|
23
|
|
|
let posts = [] |
|
|
|
|
|
|
24
|
|
|
posts = coreUtils.array.filter(files, 'path', oldPostDataPath) |
|
25
|
|
|
|
|
26
|
|
|
if(posts.length > 0 && posts[0].revisions != null) { |
|
27
|
|
|
revisions = posts[0].revisions |
|
28
|
|
|
if(revisions != null && revisions[0] != null) { |
|
29
|
|
|
json = cmsData.file.get(revisions[0].path) |
|
30
|
|
|
json = extend(true, json, req.body) |
|
31
|
|
|
|
|
32
|
|
|
delete json.abe_meta |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
abeExtend.hooks.instance.trigger('afterDuplicate', json, oldPostUrl, template, newPath, name, req, isUpdate) |
|
38
|
|
|
|
|
39
|
|
|
var pCreate = cmsOperations.create(template, newPath, name, req, json, (isUpdate) ? false : true) |
|
40
|
|
|
pCreate.then((resSave) => { |
|
41
|
|
|
if (isUpdate && oldPostUrl !== newPostUrl) { |
|
42
|
|
|
abeExtend.hooks.instance.trigger('beforeUpdate', json, oldPostUrl, template, newPath, name, req, isUpdate) |
|
43
|
|
|
cmsOperations.remove.remove(oldPostUrl) |
|
44
|
|
|
} |
|
45
|
|
|
resolve(resSave) |
|
46
|
|
|
}, |
|
47
|
|
|
() => { |
|
48
|
|
|
reject() |
|
49
|
|
|
}).catch(function(e) { |
|
50
|
|
|
console.error('[ERROR] abe-duplicate.js', e) |
|
51
|
|
|
reject() |
|
52
|
|
|
}) |
|
53
|
|
|
}) |
|
54
|
|
|
|
|
55
|
|
|
return p |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
export default duplicate |